<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 应用控制器
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class ApplicationController extends InitController{

    /**
	 * 数据查询条件过滤
	 */
    public function filter_( &$arrMap ){

		$arrMap['application_name'] = array( 'like',"%".Safe::getGpc( 'application_name' )."%" );
	}

	/**
	 * 应用程序列表
	 */
	public function bIndex_( ){

	    $this->assign( 'sid',5 );
	}

	/**
	 * 通信请求
	 */
	public function ping( ) {

		$sIp = Safe::getGpc( 'ip' );
		$sUrl = E::authcode( Safe::getGpc( 'url' ));
		$nAppId = intval( Safe::getGpc( 'id' ) );

		$arrApp = ApplicationModel::F()
		       ->query()
		       ->getAppByAppid( $nAppId );

		$nStatus = '';

		if( !empty( $arrApp['application_extra']['apppath'] ) &&
		    is_file(  $arrApp['application_extra']['apppath'].'./Api/'.$arrApp['application_apifilename'] ) ) {

            require $arrApp['application_extra']['apppath'].'./Api/'.$arrApp['application_apifilename'];

            if( class_exists( 'UcApiTest' ) ){

			    $oUcApiTest = new UcApiTest( );
			    $nStatus = $oUcApiTest->test( );
            }
		}

		else {

			$sUrl = $this->get_url_code( $arrApp,'' );

            if( $sIp =='*' ) $sIp ='';

			$nStatus = ApplicationModel::F()
		              ->query()
		              ->testApi( $sUrl, $sIp );
		}

		if( $nStatus ==1) {

			echo 'Dyhb.Dom.$(\'status_'.$nAppId.'\').innerHTML = "<img src="+TEMP_PUBLIC+"/Images/correct.gif \/><span class=\'green\'>'.G::L( '通信成功' ).'</span>";testlink();';
		}

		else {

			echo 'Dyhb.Dom.$(\'status_'.$nAppId.'\').innerHTML = "<img src="+TEMP_PUBLIC+"/Images/no_correct.gif \/><span class=\'red\'>'.G::L( '通信失败' ).'</span>";testlink();';
		}

	}

   /**
	* 添加新应用
	*/
	public function bAdd_(){

		$this->applist();
	}

   /**
	* 编辑应用
	*/
	public function bEdit_(){

		$this->applist( );
	}

   /**
	* 应用前置处理
	*/
	private  function applist( ){

		$nAppId = Safe::getGpc( 'id' );

	    $arrApp = ApplicationModel::F()
	              ->query()
	              ->getAppByAppid( $nAppId );

		$arrTagTemplates = Xml::xmlUnserialize( trim( $arrApp['application_tagtemplates'] ) );
		$arrTagTemplates = $arrTagTemplates['root'];
		$sTemplate = isset( $arrTagTemplates['template'] ) ? Input::stripslashes( htmlspecialchars( $arrTagTemplates['template'] ) ): '';

		$sTmp = '';

		if( isset( $arrTagTemplates['fields'] ) && is_array( $arrTagTemplates['fields'] ) ) {

			foreach($arrTagTemplates['fields'] as $sField => $sMemo) {

				$sTmp .= $sField.','.$sMemo."\n";
			}
		}

		$arrTagTemplates['fields'] = $sTmp;
		$this->assign( 'arrTagTemplates',$arrTagTemplates );

        $sAppPath = isset( $arrApp['application_extra']['apppath'] )? $arrApp['application_extra']['apppath'] : '';
	    $this->assign( 'sAppPath',$sAppPath );

	    $arrTypeList = array('DISCUZX'=>'DiscuzX','UCHOME'=>'UCenter Home','XSPACE'=>'X-Space','DISCUZ'=>'Discuz!','SUPESITE'=>'SupeSite','SUPEV'=>'SupeV','ECSHOP'=>'ECShop','ECMALL'=>'ECMall','OTHER'=>G::L( '其它' ) );
	    $this->assign('arrTypeList',$arrTypeList );
	}

	/**
	 * 取得测试地址
	 */
    protected function get_url_code( $arrApp,$sGetdata ) {

		$sAuthkey = $arrApp[ 'application_authkey' ];
		$sUrl = $arrApp['application_url'];
		$sApifilename = isset( $arrApp['application_apifilename']) && $arrApp['application_apifilename'] ? $arrApp['application_apifilename'] : 'Uc.php';
		$sCode = urlencode(E::authcode("action=test&".($sGetdata ? "$sGetdata&" : '')."time=".CURRENT_TIMESTAMP, 'ENCODE', $sAuthkey ) );

		return $sUrl."/Api/$sApifilename?code={$sCode}";
	 }

}
